/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarExercicio.java
 *
 * Created on 13/07/2009, 06:47:40
 */
package interfaceGraficaUsuario;

import controle.Sistema;
import util.Util;
import disciplina.Exercicio;
import java.awt.Color;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

/**
 *
 * @author João Paulo
 */
public class EditarExercicio extends javax.swing.JFrame {

    private static List<String> questoes;
    private static List<String> novasQuestoes;
    private static GregorianCalendar data = new GregorianCalendar();
    private int indiceQuestao = 0;
    

    /** Creates new form EditarExercicio */
    public EditarExercicio() {
        initComponents();
        setTitle("Editar Exercício");
        questoes = new ArrayList();
        novasQuestoes = new ArrayList();
        painelDataAtual.setText(data.getTime().toLocaleString().split(" ")[0]);
        painelDataAtual.setEnabled(false);
        painelDataAtual.setForeground(Color.BLACK);
        labelMensagem.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botaoExibir = new javax.swing.JButton();
        campoID = new javax.swing.JTextField();
        labelID = new javax.swing.JLabel();
        labelDataDeEntrega = new javax.swing.JLabel();
        labelQuestões = new javax.swing.JLabel();
        botaoProxQuestao = new javax.swing.JButton();
        botaoLimpar = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        botaoSair = new javax.swing.JButton();
        campoDataDeEntrega = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        painelDataAtual = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        textoQuestao = new javax.swing.JTextArea();
        labelDescricao = new javax.swing.JLabel();
        campoDescricao = new javax.swing.JTextField();
        labelNome = new javax.swing.JLabel();
        labelMensagem = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        botaoAddQuestao = new javax.swing.JButton();
        botaoNovaQuestao = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        botaoExibir.setText("Exibir");
        botaoExibir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExibirActionPerformed(evt);
            }
        });

        labelID.setText("ID do Exercício:");

        labelDataDeEntrega.setText("Data de entrega:");

        labelQuestões.setText("Questões:");

        botaoProxQuestao.setText("Próxima");
        botaoProxQuestao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoProxQuestaoActionPerformed(evt);
            }
        });

        botaoLimpar.setText("Limpar");
        botaoLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLimparActionPerformed(evt);
            }
        });

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        botaoSair.setText("Sair");
        botaoSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSairActionPerformed(evt);
            }
        });

        jLabel1.setText("Data atual:");

        painelDataAtual.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        painelDataAtual.setSelectionColor(new java.awt.Color(0, 0, 0));
        jScrollPane1.setViewportView(painelDataAtual);

        textoQuestao.setColumns(20);
        textoQuestao.setRows(5);
        jScrollPane2.setViewportView(textoQuestao);

        labelDescricao.setText("Descrição:");

        labelNome.setText("Nome:");

        labelMensagem.setText("Mensagem");

        botaoAddQuestao.setText("Adicionar");
        botaoAddQuestao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAddQuestaoActionPerformed(evt);
            }
        });

        botaoNovaQuestao.setText("Nova Questão");
        botaoNovaQuestao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoNovaQuestaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelID)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(111, 111, 111)
                                        .addComponent(campoID, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(botaoExibir)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 441, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 565, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(botaoLimpar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botaoAddQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botaoProxQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botaoNovaQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botaoSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botaoSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(111, 111, 111)
                                        .addComponent(labelMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(labelDataDeEntrega)
                                                .addGap(31, 31, 31)
                                                .addComponent(campoDataDeEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jScrollPane1))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(labelNome)
                                                    .addComponent(labelDescricao))
                                                .addGap(61, 61, 61)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(campoNome)
                                                    .addComponent(campoDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)))
                                .addGap(363, 363, 363)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelQuestões, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(742, 742, 742))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelID)
                    .addComponent(campoID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoExibir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelMensagem)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDescricao)
                    .addComponent(campoDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(labelDataDeEntrega)
                            .addComponent(campoDataDeEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelQuestões))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botaoProxQuestao)
                        .addGap(18, 18, 18)
                        .addComponent(botaoNovaQuestao)
                        .addGap(18, 18, 18)
                        .addComponent(botaoAddQuestao)
                        .addGap(18, 18, 18)
                        .addComponent(botaoLimpar)
                        .addGap(18, 18, 18)
                        .addComponent(botaoSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(botaoSair))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoExibirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExibirActionPerformed
        inicializaCampos();
        Exercicio exercicio = procuraExercicio(Integer.parseInt(campoID.getText()));
        labelQuestões.setText("Questões: " + exercicio.getQuestoes().size());
        campoNome.setText(exercicio.getNome());
        campoDescricao.setText(exercicio.getDescricao());
        campoDataDeEntrega.setText(exercicio.getDataEntrega().getTime().toLocaleString().split(" ")[0]);
        textoQuestao.setText("Pressione próxima questão para visualizar as questões deste exercício" + util.IO.NOVA_LINHA + "Clique em adicionar para armazenar a questão, se seguir para a próxima questão sem adicionar, ela será perdida." + util.IO.NOVA_LINHA + "Pressione Nova Questão apenas para adicionar uma questão depois da última.");
}//GEN-LAST:event_botaoExibirActionPerformed

    private void botaoProxQuestaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoProxQuestaoActionPerformed

        Exercicio exercicio = procuraExercicio(Integer.parseInt(campoID.getText()));
        textoQuestao.setText(exercicio.getQuestoes().get(indiceQuestao).toString());
        if(indiceQuestao < exercicio.getQuestoes().size()-1){
            indiceQuestao++;
        }
        

}//GEN-LAST:event_botaoProxQuestaoActionPerformed

    private void botaoLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLimparActionPerformed
        inicializaCampos();
    }//GEN-LAST:event_botaoLimparActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        try {
            if (Sistema.atualizarExercicio(Integer.parseInt(campoID.getText()), campoNome.getText(), campoDescricao.getText(), Util.criaCalendario(campoDataDeEntrega.getText()), novasQuestoes)) {
                labelMensagem.setText(Sistema.addExercicioSucesso);
                labelMensagem.setForeground(Color.BLUE);
                labelMensagem.setVisible(true);
                inicializaCampos();
            } else {
                labelMensagem.setText("ERRO. " + Sistema.excecao);
                labelMensagem.setForeground(Color.RED);
                labelMensagem.setVisible(true);
            }
        } catch (Exception e) {
            labelMensagem.setText("ERRO. " + e.getMessage());
            labelMensagem.setForeground(Color.RED);
            labelMensagem.setVisible(true);
        }
}//GEN-LAST:event_botaoSalvarActionPerformed

    private void botaoSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSairActionPerformed
        dispose();
}//GEN-LAST:event_botaoSairActionPerformed

    private void botaoAddQuestaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAddQuestaoActionPerformed
        novasQuestoes.add(textoQuestao.getText());
    }//GEN-LAST:event_botaoAddQuestaoActionPerformed

    private void botaoNovaQuestaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoNovaQuestaoActionPerformed
        textoQuestao.setText("");
        int qtdeQuestoes = novasQuestoes.size()+1;
        labelQuestões.setText("Questões: " + qtdeQuestoes);
    }//GEN-LAST:event_botaoNovaQuestaoActionPerformed
    private Exercicio procuraExercicio(int id) {
        return Sistema.getExercicio(id);
    }

    private void inicializaCampos() {
        campoNome.setText("");
        campoDescricao.setText("");
        campoDataDeEntrega.setText("");
        textoQuestao.setText("");
        questoes.clear();
        novasQuestoes.clear();
        indiceQuestao = 0;

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EditarExercicio().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAddQuestao;
    private javax.swing.JButton botaoExibir;
    private javax.swing.JButton botaoLimpar;
    private javax.swing.JButton botaoNovaQuestao;
    private javax.swing.JButton botaoProxQuestao;
    private javax.swing.JButton botaoSair;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoDataDeEntrega;
    private javax.swing.JTextField campoDescricao;
    private javax.swing.JTextField campoID;
    private javax.swing.JTextField campoNome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelDataDeEntrega;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelMensagem;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelQuestões;
    private javax.swing.JTextPane painelDataAtual;
    private javax.swing.JTextArea textoQuestao;
    // End of variables declaration//GEN-END:variables
}
